package com.adams.action.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.adams.access.db.bean.Icon;
import com.adams.access.db.bean.Menu;
import com.adams.access.db.bean.Shortcut;
import com.adams.common.BaseAction;
import com.adams.common.Certificate;
import com.adams.custom.page.mgt.service.ViewParseService;
import com.adams.role.service.RoleService;
import com.adams.sys.icon.service.IconService;
import com.adams.sys.menu.service.MenuService;
import com.adams.sys.shortcut.service.ShortcutService;

/**
 * 
 * 
 * File Name : ShortcutAction.java
 * 
 * @Description : 快捷菜单action
 * @author 904304 张益民
 */
public class ShortcutAction extends BaseAction
{

	private MenuService menuService;

	private RoleService roleService;

	private ShortcutService shortcutService;

	private IconService iconService;

	private ViewParseService viewParseService;

	/**
	 * 
	 * Description : 显示布局
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	public String listPanel() throws Exception
	{
		long menuId = request.getParameter("menuId") != null ? Long
				.valueOf(request.getParameter("menuId")) : 0;
		write(viewParseService.getParseViewJSONByMenuId(getUserId(), menuId));
		return null;
	}

	/**
	 * 
	 * Description : 查询权限
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getRights() throws Exception
	{
		// 获取权限
		List<Menu> rightList = Certificate.getRights(request);
		// 获取快捷菜单
		List<Shortcut> shortcutList = shortcutService.findByUserId(Certificate
				.getUser(request).getId());

		List<Menu> list = menuService.querySubMenus(Long.valueOf("0"));
		if (list != null && list.size() > 0)
		{
			List<Map> result = new ArrayList<Map>();
			for (Menu menu : list)
			{
				if (!visible(rightList, menu))
				{
					continue;
				}

				Map map = new HashMap();
				setMenuMap(shortcutList, rightList, map, menu);

				result.add(map);

			}
			write(JSONArray.fromObject(result).toString());
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	private void setMenuMap(List<Shortcut> shortcutList, List<Menu> rightList,
			Map map, Menu menu)
	{
		map.put("id", menu.getId());
		map.put("text", menu.getMenuName());
		map.put("shortcutAlias", null);
		map.put("icon", null);
		map.put("tabTip", "");
		map.put("checked", isChecked(shortcutList, menu));

		// 子节点
		List<Menu> subMenuList = menuService.querySubMenus(menu.getId());
		if (subMenuList != null && subMenuList.size() > 0)
		{
			List<Map> children = new ArrayList<Map>();
			for (Menu m : subMenuList)
			{
				if (!visible(rightList, m))
				{
					continue;
				}

				Map subMap = new HashMap();
				setMenuMap(shortcutList, rightList, subMap, m);
				children.add(subMap);
			}
			map.put("leaf", false);
			map.put("children", children);

		} else
		{
			map.put("leaf", true);
		}
	}

	/**
	 * 
	 * Description : 保存当前用户快捷菜单
	 * 
	 * 
	 */
	@SuppressWarnings("unchecked")
	public void saveShortcut()
	{
		String menuIds = request.getParameter("menuIds");
		String[] menuIdArr =
		{};
		if (menuIds != null && !menuIds.equals(""))
		{
			menuIdArr = menuIds.split(",");
		}
		List<Shortcut> shortcutList = new ArrayList<Shortcut>();
		Long userId = Certificate.getUser(request).getId();
		Shortcut shortcut;
		for (String menuId : menuIdArr)
		{
			shortcut = new Shortcut();
			shortcut.setUserId(userId);
			shortcut.setMenuId(Long.parseLong(menuId));

			shortcutList.add(shortcut);
		}

		shortcutService.saveShortcut(shortcutList, userId);

		Map result = new HashMap();
		result.put("success", true);
		result.put("msg", "设置成功！");
		write(JSONObject.fromObject(result).toString());

	}

	/**
	 * 
	 * Description : 获取快捷菜单
	 * 
	 * 
	 */
	@SuppressWarnings("unchecked")
	public void getShortcut()
	{
		// 获取权限
		List<Menu> rightList = Certificate.getRights(request);
		// 获取快捷菜单
		List<Shortcut> shortcutList = shortcutService.findByUserId(Certificate
				.getUser(request).getId());

		Map result = new HashMap();
		List<Map> data = new ArrayList<Map>();
		for (Shortcut shortcut : shortcutList)
		{
			Menu menu = getMenuOfShortcut(shortcut, rightList);
			if (menu != null)
			{
				Map map = new HashMap();
				map.put("id", menu.getId());
				map.put("topId", Certificate.getTopMenu(rightList, menu));
				map.put("text", menu.getMenuName());
				map.put("shortcutAlias", null);
				map.put("tips", menu.getTips());
				map.put("leaf", true);
				if (menu.getUrl().indexOf("?") >= 0)
				{
					map.put("url", menu.getUrl() + "&menuId=" + menu.getId());
				} else
				{
					map.put("url", menu.getUrl() + "?menuId=" + menu.getId());
				}

				Icon icon = iconService.queryById(Long
						.valueOf(menu.getIconId()));
				if (icon != null)
				{
					map.put("icon", icon.getUrl());
				}
				data.add(map);
			}
		}
		result.put("data", data);
		result.put("success", true);
		write(JSONObject.fromObject(result).toString());
	}

	/**
	 * 
	 * Description : 获取快捷方式具体菜单信息
	 * 
	 * @param shortcut
	 * @param rightList
	 * @return
	 * 
	 */
	private Menu getMenuOfShortcut(Shortcut shortcut, List<Menu> rightList)
	{
		for (Menu menu : rightList)
		{
			if (shortcut.getMenuId().equals(menu.getId()))
			{
				return menu;
			}
		}
		return null;
	}

	/**
	 * 
	 * Description : 检查权限资源是否选中
	 * 
	 * @param code
	 * @return boolean
	 * 
	 */
	private boolean isChecked(List<Shortcut> shortcutList, Menu menu)
	{
		for (Shortcut shortcut : shortcutList)
		{
			if (shortcut.getMenuId().equals(menu.getId()))
			{
				return true;
			}
		}
		return false;
	}

	/**
	 * 
	 * Description : 控制菜单是否可见
	 * 
	 * @param rightList
	 * @param menu
	 * @return
	 * 
	 */
	private boolean visible(List<Menu> rightList, Menu menu)
	{
		if (!menu.getVisible())
		{
			return false;
		}
		for (Menu m : rightList)
		{
			if (m.getMenuCode().equals(menu.getMenuCode()))
			{
				return true;
			}
		}
		return false;
	}

	public MenuService getMenuService()
	{
		return menuService;
	}

	public void setMenuService(MenuService menuService)
	{
		this.menuService = menuService;
	}

	public RoleService getRoleService()
	{
		return roleService;
	}

	public void setRoleService(RoleService roleService)
	{
		this.roleService = roleService;
	}

	public ShortcutService getShortcutService()
	{
		return shortcutService;
	}

	public void setShortcutService(ShortcutService shortcutService)
	{
		this.shortcutService = shortcutService;
	}

	public IconService getIconService()
	{
		return iconService;
	}

	public void setIconService(IconService iconService)
	{
		this.iconService = iconService;
	}

	public ViewParseService getViewParseService()
	{
		return viewParseService;
	}

	public void setViewParseService(ViewParseService viewParseService)
	{
		this.viewParseService = viewParseService;
	}

}
